/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.structuralload;

import org.openaecfm.model.structuralload.FailureConnectionCondition;
import org.openaecfm.model.structuralload.StructuralConnectionCondition;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.RealParameter;
import org.openaecfm.spf.model.SimpleEntityInstance;

public final class FailureConnectionConditionIO extends
		AbstractEntityInstanceInputImpl<FailureConnectionCondition> implements
		IEntityInstanceOutput<FailureConnectionCondition> {
	
	private IEntityInstanceUtilInput<StructuralConnectionCondition> utilInput;
	private IEntityInstanceUtilOutput<StructuralConnectionCondition> utilOutput;

	public void setUtilOutput(
			IEntityInstanceUtilOutput<StructuralConnectionCondition> utilOutput) {
		this.utilOutput = utilOutput;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(FailureConnectionCondition pojo,
			IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance(
				"IFCFAILURECONNECTIONCONDITION", appDataManager.getName(pojo));

		utilOutput.exportSPF(pojo, ei, appDataManager);

		// Set TensionFailureX
		if (pojo.getTensionFailureX() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getTensionFailureX()));
		}

		// Set TensionFailureY
		if (pojo.getTensionFailureY() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getTensionFailureY()));
		}

		// Set TensionFailureZ
		if (pojo.getTensionFailureZ() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getTensionFailureZ()));
		}

		// Set CompressionFailureX
		if (pojo.getCompressionFailureX() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getCompressionFailureX()));
		}

		// Set CompressionFailureY
		if (pojo.getCompressionFailureY() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getCompressionFailureY()));
		}

		// Set CompressionFailureZ
		if (pojo.getCompressionFailureZ() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getCompressionFailureZ()));
		}

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected FailureConnectionCondition doCreateObject() {
		return new FailureConnectionCondition();
	}

	public void setUtilInput(
			IEntityInstanceUtilInput<StructuralConnectionCondition> utilInput) {
		this.utilInput = utilInput;
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			FailureConnectionCondition pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set TensionFailureX
		Parameter tfx = sei.getParameter(1);
		if (tfx.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) tfx;
			pojo.setTensionFailureX(r.toDouble());
		}

		// Set TensionFailureY
		Parameter tfy = sei.getParameter(2);
		if (tfy.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) tfy;
			pojo.setTensionFailureY(r.toDouble());
		}

		// Set TensionFailureZ
		Parameter tfz = sei.getParameter(3);
		if (tfz.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) tfz;
			pojo.setTensionFailureZ(r.toDouble());
		}

		// Set CompressionFailureX
		Parameter cfx = sei.getParameter(4);
		if (cfx.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) cfx;
			pojo.setCompressionFailureX(r.toDouble());
		}

		// Set CompressionFailureY
		Parameter cfy = sei.getParameter(5);
		if (cfy.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) cfy;
			pojo.setCompressionFailureY(r.toDouble());
		}

		// Set CompressionFailureZ
		Parameter cfz = sei.getParameter(6);
		if (cfz.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) cfz;
			pojo.setCompressionFailureZ(r.toDouble());
		}
	}

	@Override
	public void resolveReferenceParameter(EntityInstance ei,
			FailureConnectionCondition pojo, IAppDataManager appDataManager) {
		return ;
	}
}
